package br.com.portalaberto.connection.chain;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Image;

import org.json.me.JSONException;
import org.json.me.JSONObject;

public class JSONHandler implements Handler{

	private String comentario;
	private String imagem;
	private String strJSON = null;
	private double latitude;
	private double longitude;
	
	public JSONHandler(String comentario,String imagem,double latitude,double longitude){
		this.comentario = comentario;
		this.imagem = imagem;
		this.latitude = latitude;
		this.longitude = longitude;
	}
	
	
	
	public void prepareHeaders(HttpConnection c) throws Exception {
		System.out.println("entrando no prepare headers do jsonhandler");
		criarJSON();
		//c.setRequestProperty( "json", "lucasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" );
		c.setRequestProperty( "json", "Tem um json no corpo");
		 
		/*try {
			
			InputStream is = c.openInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int data = 0;
			while ((data = is.read()) != -1) {
				baos.write(data);
			}
			strJSON = new String(baos.toByteArray());
					    
		    if ( strJSON == null || strJSON.equals("") ) {
		      // Ignore
		    } else {
		      c.setRequestProperty( "json", strJSON );
		    }
		    System.out.println("json " + strJSON );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
		
	}

	public boolean processHeaders(HttpConnection c) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void criarJSON(){
				
		JSONObject jobject = null;
		JSONObject jnoticia = null;
		JSONObject jcoordenadas = null;
		
	  
	    try {
	    	jobject = new JSONObject();
			jnoticia = new JSONObject();
			jcoordenadas = new JSONObject();
	    	//aqui eu passo as coordenadas pro objeto tb
	        jobject.put("noticia", jnoticia); 
	        jnoticia.put("comentario",comentario);
			jnoticia.put("imagem",imagem);
	        jobject.put("coordenadadas",jcoordenadas);
	        jcoordenadas.put("latitude", latitude);
	        jcoordenadas.put("longitude", longitude);
	        strJSON = jobject.toString();
	        
			System.out.println(" JSON OBJECT "+jobject.toString());
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public String imageToString(Image image){
		String strImg = null;
		
		return strImg;
	}
	



}
